
import 'package:flutter/cupertino.dart';
import 'package:flutter_mvc/flutter_mvc.dart';

///mvc管理器+管理原生的生命周期
class MvcManager extends WidgetsBindingObserver{
  var isPauseAndResume=true;


  MvcManager(){

    WidgetsBinding.instance!.addObserver(this);
  }

  List<BaseController?> _list=[];
  List<BaseController?> get controllers=>_list;

  List<Function(BaseController?) > _onListeners=[];
  ///当前正在显示的controller
  BaseController? currentController;

  static MvcManager? _instance;

  static MvcManager? get instance{
    if(_instance==null){
      _instance=MvcManager();
    }
    return _instance;
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if(currentController==null){
      return;
    }
    if(state==AppLifecycleState.resumed){
      BaseController.getMvcAttribute(currentController!).state.onResume();
    }else if(state==AppLifecycleState.paused){
      BaseController.getMvcAttribute(currentController!).state.onPause();
    }
  }


  void addController(BaseController? controller){
    _list.add(controller);
    resume(controller);
  }

  void removeController(BaseController? controller){
    assert(_list.contains(controller));
    _list.remove(controller);

    if(_list.length==0){
      WidgetsBinding.instance!.removeObserver(this);
    }else{
      resume(_list.last);
    }
  }
  
  void addListener(Function(BaseController?) listener){
    _onListeners.add(listener);
  }
  
  
  void removeListener(Function(BaseController) listener){
    if(_onListeners.contains(listener))
    _onListeners.remove(listener);
  }
  
  ///活跃的
  void resume(BaseController? controller){
    currentController=controller;

  }

  ///页面活跃的
  void pageResume(BaseController? controller){
    for (var value in _onListeners) {
      value(controller);
    }
  }

}